package com.sky.controller.admin;

import com.sky.dto.DishDTO;
import com.sky.dto.DishPageQueryDTO;
import com.sky.entity.Dish;
import com.sky.result.PageResult;
import com.sky.result.Result;
import com.sky.service.DishService;
import com.sky.vo.DishVO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.cache.CacheProperties;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Set;

@RestController
@RequestMapping("/admin/dish")
@Slf4j
public class DishController {

    @Autowired
    private DishService dishService;

    @Autowired
    private RedisTemplate redisTemplate;

    /**
     * 新增功能接口
     * @param dish
     * @return
     */
    @PostMapping
    public Result save(@RequestBody DishDTO dish){

        log.info("save dish:{}", dish);
        dishService.save(dish);
        return Result.success();
    }

    /**
     * 分页查询菜品信息
     * @param dishPageQueryDTO
     * @return
     */
    @GetMapping("/page")
    public Result<PageResult> page(DishPageQueryDTO dishPageQueryDTO){

        log.info("分页查询菜品:{}", dishPageQueryDTO);
        PageResult pageResult= dishService.page(dishPageQueryDTO);
        return Result.success(pageResult);

    }

    /**
     * 根据id数组批量或者单个删除菜品信息
     * @param ids
     * @return
     */
    @DeleteMapping
    public Result delete(@RequestParam List<Long> ids){
        log.info("delete dish:{}", ids);

        cleanCache("dish_*");

        dishService.delete(ids);
        return Result.success();
    }

    /**
     * 根据id查询菜品信息
     * @param id
     * @return
     */
    @GetMapping("/{id}")
    public Result<DishVO> getDishById(@PathVariable Long id){

        log.info("get dish:{}", id);
        DishVO dishVO= dishService.getById(id);
        return Result.success(dishVO);

    }

    /**
     * 根据id修改菜品信息
     * 当修改信息涉及到分类时，将影响到两份缓存数据。我们将逻辑简化为删除所有的缓存数据
     * @param dishDTO
     * @return
     */
    @PutMapping
    public Result update(@RequestBody DishDTO dishDTO){

        log.info("update dish:{}", dishDTO);
        dishService.update(dishDTO);

        cleanCache("dish_*");

        return Result.success();

     }

    /**
     * 根据分类id查询数据
     * @param categoryId
     * @return
     */
     @GetMapping("/list")
     public Result<List<Dish>> getDishByCategoryId(@RequestParam Long categoryId){
        log.info("get dish:{}", categoryId);
        List<Dish> dishes=dishService.getByCategoryId(categoryId);
        return Result.success(dishes);
     }

    /**
     * 根据id修改菜品的状态
     * @param status
     * @param id
     * @return
     */
    @PostMapping("/status/{status}")
    public Result status(@PathVariable("status") Integer status,Long id){
        log.info("修改菜品的状态:{},{}",status,id);
        dishService.status(status,id);

        cleanCache("dish_*");

        return Result.success();
    }

    /**
     * 将清理缓存的逻辑抽取为一个私有的类内公用方法
     * @param pattern
     */
    private void cleanCache(String pattern){
        Set keys=redisTemplate.keys(pattern);
        redisTemplate.delete(keys);
    }

}
